package com.h2View.utils;

import java.net.URL;

/**
 * @author jack
 * @author suisui
 * @version 1.0
 * @since JavaFX2.0 JDK1.8
 */
public class StringUtil {

    public static boolean isEmpty(String src){
        return src == null || src.length() == 0 || src.trim().length() == 0;
    }

    public static boolean isNotEmpty(String src){
        return src != null && src.trim().length()>0;
    }

    public static boolean isEmpty(Object src){
        if(src == null ){
            return true;
        }
        if(src instanceof String){
           return isEmpty((String) src);
        }
        return false;
    }

    public static String getRootPath(URL url) {
        String fileUrl = url.getFile();
        int pos = fileUrl.indexOf('!');

        if (-1 == pos) {
            return fileUrl;
        }

        return fileUrl.substring(5, pos);
    }

    public static String dotToSplash(String name) {
        return name.replaceAll("\\.", "/");
    }


    public static String splashToDot(String name) {
        return name.replaceAll("/", "\\.");
    }

    public static String trimExtension(String name) {
        int pos = name.lastIndexOf('.');
        if (-1 != pos) {
            return name.substring(0, pos);
        }

        return name;
    }

    public static String trimURI(String uri) {
        String trimmed = uri.substring(1);
        int splashIndex = trimmed.indexOf('/');

        return trimmed.substring(splashIndex);
    }

    public static String getBaseClassName(String name) {
        int index = name.indexOf("$");
        if (index == -1) {
            return name;
        }
        return name.substring(0, index);
    }


    public static String toInstanceName(String name) {
        return name.substring(0, 1).toLowerCase().concat(name.substring(1));
    }

    public static String toClassName(String name) {
        return name.substring(0, 1).toUpperCase().concat(name.substring(1));
    }

    public static String getFilePathInResources(String name) {
        String resources = "resources";
        int resIdx = name.indexOf(resources);
        if (resIdx == -1) {
            return name;
        }
        return name.substring(resIdx + resources.length() + 1);
    }

    public static String getFileBaseName(String name) {
        String result;
        String[] tempStrs = name.split("/");
        if (1 == tempStrs.length) { //只有文件名，即name: languageDemo.fxml
            result = StringUtil.trimExtension(name);
        } else {
            result = StringUtil.trimExtension(tempStrs[tempStrs.length - 1]);
        }
        return result;
    }

    public static boolean isNumeric(String cs) {
        // 判断是否为空，如果为空则返回false
        if (isEmpty(cs)) {
            return false;
        }
        // 通过 length() 方法计算cs传入进来的字符串的长度，并将字符串长度存放到sz中
        final int sz = cs.length();
        // 通过字符串长度循环
        for (int i = 0; i < sz; i++) {
            // 判断每一个字符是否为数字，如果其中有一个字符不满足，则返回false
            if (!Character.isDigit(cs.charAt(i))) {
                return false;
            }
        }
        // 验证全部通过则返回true
        return true;
    }

}